package com.wutianyi.module.company;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import com.wutianyi.module.category.Category;
import com.wutianyi.module.commons.PropertyGroup;
import com.wutianyi.utils.Utils;

/**
 * @author wutianyi
 * 
 */
public class Company implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -8239189864601232698L;
	
	private transient int id;
	private String companyId;
	
	private String name;
	/**
	 * 英文名称，用于url的显示
	 */
	private String nameEn;
	private String description;
	
	// FIXME!
	private Category category = new Category(1);
	private int exCategoryId;
	
	private Date gmtCreate;
	private Date gmtModified;
	
	transient List<PropertyGroup> pgs;
	
	transient Map<String, PropertyGroup> indexPgs;
	
	transient Set<Integer> configIds;
	
	public Company(String companyId, String name, String description) {
		this.companyId = companyId;
		this.name = name;
		this.description = description;
		
		pgs = new ArrayList<PropertyGroup>();
		
		this.gmtCreate = new Date();
		this.gmtModified = new Date();
	}
	
	public Company() {
		pgs = new ArrayList<PropertyGroup>();
	}
	
	public void addPropertyGroup(PropertyGroup pg) {
		this.pgs.add(pg);
	}
	
	public String getCompanyId() {
		return companyId;
	}
	
	public void setCompanyId(String companyId) {
		this.companyId = companyId;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getDescription() {
		return description;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}
	
	public Date getGmtCreate() {
		return gmtCreate;
	}
	
	public void setGmtCreate(Date gmtCreate) {
		this.gmtCreate = gmtCreate;
	}
	
	public Date getGmtModified() {
		return gmtModified;
	}
	
	public void setGmtModified(Date gmtModified) {
		this.gmtModified = gmtModified;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public List<PropertyGroup> getPgs() {
		return pgs;
	}
	
	public void setPgs(List<PropertyGroup> pgs) {
		this.pgs = pgs;
	}
	
	public Category getCategory() {
		return category;
	}
	
	public void setCategory(Category category) {
		this.category = category;
	}
	
	public int getExCategoryId() {
		return exCategoryId;
	}
	
	public void setExCategoryId(int exCategoryId) {
		this.exCategoryId = exCategoryId;
	}
	
	public String getNameEn() {
		return nameEn;
	}
	
	public void setNameEn(String nameEn) {
		this.nameEn = nameEn;
	}
	
	/**
	 * @return
	 */
	public Set<Integer> getConfigIds() {
		if (null == configIds) {
			configIds = new HashSet<Integer>();
		}
		
		if (null != pgs && pgs.size() > 0 && configIds.size() == 0) {
			
			for (PropertyGroup pg : pgs) {
				configIds.add(pg.getParentId());
			}
		}
		
		return configIds;
	}
	
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("[\n\tid: ");
		builder.append(id);
		builder.append("\n\tcompanyId: ");
		builder.append(companyId);
		builder.append("\n\tname: ");
		builder.append(name);
		builder.append("\n\tcategoryId: ");
		builder.append(category.getId());
		builder.append("\n\texCategoryId: ");
		builder.append(exCategoryId);
		builder.append("\n\tgmtCreate: ");
		builder.append(gmtCreate);
		builder.append("\n\tgmtModified: ");
		builder.append(gmtModified);
		
		if (!pgs.isEmpty()) {
			builder.append("\n\tpgs:\n\t{");
			for (PropertyGroup pg : pgs) {
				builder.append(pg);
			}
			
			builder.append("\n\t}");
		}
		
		builder.append("\n]");
		return builder.toString();
	}
	
	public int hashCode() {
		HashFunction hf = Hashing.md5();
		HashCode hc = hf.newHasher().putString(companyId).putString(Utils.stringToString(name))
				.putString(Utils.stringToString(nameEn))
				.putString(Utils.stringToString(description)).putInt(category.getId())
				.putInt(exCategoryId).hash();
		return hc.asInt();
	}
}
